programming4us
           
 
 
Windows Phone

Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Show Me Something on Screen

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/1/2010 5:38:32 PM
Let’s expand on those projects and show new and hopefully interesting things instead! Start by creating a new game project in Visual Studio and add a new variable to the list of variables the project already has defined, as shown in the following:
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture;

Texture2D is a class that holds any images you might want to render on screen. Also notice the other two variables declared by default, namely the SpriteBatch and GraphicsDeviceManager.

The GraphicsDeviceManager is discussed in more detail in later chapters, but the SpriteBatch object is what you use to do the actual rendering. This object is discussed in depth throughout this chapter, but before you learn about the intricacies of rendering 2D objects, first, you shouls just get something showing on the screen.

You need an image to draw to the screen. In your Visual Studio solution, notice that you have two projects: your code project where your game is and a content project next to it. This content project is where you add the images you want to draw and it is discussed in more detail in later chapters. Right-click your content project now, and then choose Add->New Item. Feel free to pick an image for your computer (ensure the image is a common image format such as jpg, bmp, png, or gif), or use one from the downloadable examples. The example included with the downloadable examples uses the file glacier.jpg, which is used in some of the samples you can download for Game Studio 4.0.

Content Item Properties

Selecting an item in a content project (such as the image you just added) and viewing its properties show you many different options depending on the type of item you have selected. For now, the only one that is important is Asset Name, which, by default, is the name of the file you’ve added to the content project without the extension. In the case of the code in the downloadable examples, it is glacier. The other properties are explained in more detail in later chapters.


Getting something on screen is remarkably easy from here. First, you need to scroll through your project’s game1.cs file and find the LoadContent method. As the name implies, this is where you load the content for your game, so add the following line of code to that method:

texture = Content.Load<Texture2D>("glacier");

If you used a different image other than the glacier image this example uses, you need to enter that filename (without the extension) instead. This takes the image data from your picture and loads it into the Texture2D object you specified. The Content object is an instance of the ContentManager, which was automatically created by the new project. Again, the content manager is discussed in more depth later, but for now, it is the thing that loads your content.

All that is left is to draw your image on the screen. Look through your project to find the Draw method and replace the method body with the following code:

GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(texture, GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.End();
base.Draw(gameTime);

The first and last lines are the same as what is already in your project, whereas the middle three lines are where the actual drawing of your image is. Run the project and you should see an image similar to the one shown in Figure 1 if you used the glacier image provided with the downloadable examples or an image you chose that covered the entire window if you did not use the glacier image.

Figure 1. A 2D image drawn on screen


This is an example of this simplicity. With a mere five lines of code, you’ve done more work than you probably realize, rendering your image on to the screen. With the instant gratification of seeing something, now it’s time to take a step back and see what has gone into rendering this picture.

Other -----------------
- Windows Phone 7 : Working with SharePoint Documents
- Windows Phone 7 : Connecting to SharePoint
- Windows Phone 7 : Synching Notes to the Web
- Windows Phone 7 : Using OneNote Mobile
- Windows Phone 7 : Using PowerPoint Mobile
- Windows Phone 7 : Using Excel Mobile
- Windows Phone 7 : Using Word Mobile
- Windows Phone 7 : Saving and Deleting Documents
- Windows Phone 7 : Sharing Documents via E-Mail
- Windows Phone 7 : Opening Documents
- Windows Phone 7 : Managing Storage on Your Phone
- Windows Phone 7 : Changing Zune Sync Settings
- Windows Phone 7 : Synching with Your PC - Adding Media to Your Zune Collection
- Windows Phone 7 : Synching Files Wirelessly
- Windows Phone 7 : Synching with Your PC - Seeing What’s Synching
- Windows Phone 7 : Synching Media with Your Phone
- Windows Phone 7 : Synching with Your PC - Touring the Zune Software
- Windows Phone 7 : Talking to Your Phone
- Windows Phone 7 : Changing Keyboard Settings
- Windows Phone 7 : Customizing the Dictionary
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us